﻿Public Class frmCourseModify
    Dim mybind As BindingManagerBase
    Private Sub frmCourseModify_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim mytable As Data.DataTable
        Dim recordnum As Integer
        txtSQL = "SELECT * FROM 课程"
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)
        mytable = DBSet.Tables.Item(0)

        txtCourse_NO.DataBindings.Add("Text", mytable, "课程编号")

        txtCourse_name.DataBindings.Add("Text", mytable, "课程名称")

        txtCourse_type.DataBindings.Add("Text", mytable, "课程类别")

        txtCourse_credit.DataBindings.Add("Text", mytable, "学分")

        txtCourse_des.DataBindings.Add("Text", mytable, "课程描述")
        mybind = CType(Me.BindingContext(mytable), CurrencyManager)
        mybind.Position = 0
        txtCourse_NO.ReadOnly = True
    End Sub
    '
    Private Sub cmdFirst_Click(sender As Object, e As EventArgs) Handles cmdFirst.Click
        mybind.Position = 0
    End Sub
    '
    Private Sub cmdForward_Click(sender As Object, e As EventArgs) Handles cmdForward.Click
        If (mybind.Position = 0) Then
            MessageBox.Show("已经到了第一条记录!", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            mybind.Position = mybind.Position - 1
        End If
    End Sub
    '
    Private Sub cmdBackward_Click(sender As Object, e As EventArgs) Handles cmdBackward.Click
        If mybind.Position = mybind.Count - 1 Then
            MessageBox.Show("已经到了最后一条记录!", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            mybind.Position = mybind.Position + 1
        End If
    End Sub

    Private Sub cmdLast_Click(sender As Object, e As EventArgs) Handles cmdLast.Click
        mybind.Position = mybind.Count - 1
    End Sub

    Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
        '从数据库表中修改记录
        Dim recordnum As Integer
        Dim i As Integer = mybind.Position

        '判断是否输入了课程编号
        If Trim(txtCourse_NO.Text) = "" Then
            MsgBox("请输入课程编号", vbOKOnly, "警告")
            txtCourse_NO.Focus()
            Exit Sub
        End If

        '判断是否输入了课程名称
        If txtCourse_name.Text = "" Then
            MsgBox("请输入课程名称", vbOKOnly, "警告")
            txtCourse_name.Focus()
            Exit Sub
        End If

        '判断是否输入了课程类别
        If txtCourse_type.Text = "" Then
            MsgBox("请输入课程类别", vbOKOnly, "警告")
            txtCourse_type.Focus()
            Exit Sub
        End If

        '判断是否输入了学分
        If txtCourse_credit.Text = "" Then
            MsgBox("请输入学分", vbOKOnly, "警告")
            txtCourse_credit.Focus()
            Exit Sub
        End If

        '判断学分是否为数值型
        If Not IsNumeric(txtCourse_credit.Text) Then
            MsgBox("请输入正确的学分", vbOKOnly, "警告")
            txtCourse_credit.Focus()
            Exit Sub
        End If

        '判断是否输入了课程描述
        If txtCourse_des.Text = "" Then
            MsgBox("请输入课程描述", vbOKOnly, "警告")
            txtCourse_des.Focus()
            Exit Sub
        End If

        txtSQL = "UPDATE 课程 SET"
        txtSQL = txtSQL & "课程名称='" & txtCourse_name.Text & "',"
        txtSQL = txtSQL & "课程类别='" & txtCourse_type.Text & "',"
        txtSQL = txtSQL & "学分='" & txtCourse_credit.Text & "',"
        txtSQL = txtSQL & "课程描述='" & txtCourse_des.Text & "'"
        txtSQL = txtSQL & "WHERE 课程编号='" & txtCourse_NO.Text & "'"
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)
        If recordnum > 0 Then
            MsgBox("更新完成!")
        Else
            MsgBox("更新失败!" & ErrorMsg)
        End If

        '从DataSet中更新指定记录
        DBSet.Tables(0).Rows(mybind.Position).EndEdit()
        DBSet.Tables(0).AcceptChanges()
        mybind.Position = i
    End Sub

    Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
        '从数据库表中删除记录
        Dim recordnum As Integer
        txtSQL = "DELETE 课程 WHERE 课程编号='" & txtCourse_NO.Text & "'"
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)

        '从DataSet中删除指定记录
        DBSet.Tables(0).Rows(mybind.Position).Delete()
        DBSet.Tables(0).AcceptChanges()
    End Sub

    Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
End Class